#include <iostream>
#include "UF.h"
#include "TestSortHelper.h"

using namespace std;

void testCurrent() {
    int n = 10;

    UFimp1* uf = new UFimp1(10);

    int ints[8][2] = {{4,3},{3,8},{6,5},{9,4},{2,1},{5,0},{7,2},{6,1}};

    for (int i = 0; i < 8; ++i) {
        uf->unioned(ints[i][0],ints[i][1]);
    }

    cout<<"connected components:"<<uf->count()<<endl;

    delete uf;
}

void testUnioned(string ufimp, UF* uf, int * arr, int n) {
    clock_t startTime = clock();
    for (int i = 0; i < n; i+=2) {
        uf->unioned(arr[i],arr[i+1]);
    }
    clock_t endTime = clock();
    cout << ufimp << " unioned " << n/2 << " times cust " << ":" << double(endTime - startTime) / CLOCKS_PER_SEC << " s" << endl;
}

void testConnected(string ufimp, UF* uf, int * arr, int n) {
    clock_t startTime = clock();
    for (int i = 0; i < n; i+=2) {
        uf->connected(arr[i],arr[i+1]);
    }
    clock_t endTime = clock();
    cout << ufimp << " connected " << n << " times cust " << ":" << double(endTime - startTime) / CLOCKS_PER_SEC << " s" << endl;

}

int main() {
//    testCurrent();
    int n = 20000000;
    UF* uf1 = new UFimp1(n/2);
    UF* uf2 = new UFimp2(n/2);
    UF* uf3 = new UFimp3(n/2);
    UF* uf4 = new UFimp4(n/2);
    UF* uf5 = new UFimp5(n/2);
    int* arr = TestSortHelper::generateRandomArray(n,0,(n/2)-1);
//    testUnioned("UFimp1",uf1,arr,n);
//    testUnioned("UFimp2",uf2,arr,n);
//    testUnioned("UFimp3",uf3,arr,n);
    testUnioned("UFimp4",uf4,arr,n);
    testUnioned("UFimp5",uf5,arr,n);
    int* arr2 = TestSortHelper::generateRandomArray(n,0,(n/2)-1);
//    testConnected("UFimp1",uf1,arr2,n);
//    testConnected("UFimp2",uf2,arr2,n);
//    testConnected("UFimp3",uf3,arr2,n);
    testConnected("UFimp4",uf4,arr2,n);
    testConnected("UFimp5",uf5,arr2,n);

//    cout<<"uf1.counts = " <<  uf1->count();
//    cout<<" uf2.counts = " << uf2->count();
//    cout<<" uf3.counts = " << uf3->count();
    cout<<" uf4.counts = " << uf4->count();
    cout<<" uf5.counts = " << uf5->count();
    cout<< endl;

    delete arr;
    delete arr2;
    delete uf1;
    delete uf2;
    delete uf3;
    delete uf4;
}